package com.weather.wangs.weather.utils;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wangs on 2015/11/10.
 */
public class UtilGson {
    private static Gson gson;

    public static <T> T httpFormat(String jsonString, Class<T> clazz) {
        T gsonBean = null;
        try {
            if (jsonString == null) {
                return null;
            }
            if (gson == null) {
                gson = new Gson();
            }
            gsonBean = gson.fromJson(jsonString, clazz);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return gsonBean;
    }

    public static <T> List<T> httpFormatList(String jsonString,
                                             TypeToken<List<T>> token) {
        List<T> list = null;
        try {
            if (jsonString == null) {
                return null;
            }
            if (gson == null) {
                gson = new Gson();
            }
            list = gson.fromJson(jsonString, token.getType());
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (list == null) {
            list = new ArrayList<>();
        }
        return list;
    }
}
